package yuejian_07.yuejian_07.model;

import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.activity.ConSearchActivity;
import yuejian_07.yuejian_07.javabean.Contest;
import yuejian_07.yuejian_07.service.ContestService;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static yuejian_07.yuejian_07.base.Constant.BASE_URL;


public class ContestModel {
    private Retrofit retrofit;
    public ContestModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取活动列表
    public void contestlist( final RetrofitListener listener){
        ContestService contestService=retrofit.create(ContestService.class);
        Call call=contestService.getAllContests();
        Callback<List<Contest>> callback=new Callback<List<Contest>>(){
            @Override
            public void onResponse(Call<List<Contest>> call, Response<List<Contest>> response ){
                List<Contest> data=response.body();
                listener.onSuccess(data);
            }
            @Override
            public  void onFailure(Call<List<Contest>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //获取活动详情
    public void getContestById(int Contest_id, final RetrofitListener listener){
        ContestService contestService=retrofit.create(ContestService.class);
        Call call=contestService.getContestById(Contest_id);
        Callback<Contest> callback=new Callback<Contest>(){
            @Override
            public void onResponse(Call<Contest> call, Response<Contest> response){

                listener.onSuccess(response.body());
            }
            @Override
            public void onFailure(Call<Contest> call,Throwable t){
                t.printStackTrace();
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    public void getConBySearch(String SearchKey, final ConSearchActivity listener){
        ContestService contestService=retrofit.create(ContestService.class);
        Call call=contestService.getContestBySearch(SearchKey);
        Callback<List<Contest>> callback=new Callback<List<Contest>>(){
            @Override
            public void onResponse(Call<List<Contest>> call, Response<List<Contest>> response ){
                List<Contest> data=response.body();
                listener.onSuccess(data);
            }
            @Override
            public  void onFailure(Call<List<Contest>> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
